<template>
  <div @click="selectDeptVisible = false">
    <el-dialog :visible="dialogVisible" v-bind="$attrs" title="新增员工" width="500px" v-on="$listeners" @open="onOpen" @close="close">
      <el-form ref="formRef" :model="form" :rules="rules" size="medium" label-width="120px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入姓名" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{width: '100%'}"
            placeholder="请选择入职时间"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="form.formOfEmployment" placeholder="请选择聘用形式" clearable :style="{width: '100%'}">
            <el-option
              v-for="(item, index) in formOfEmploymentOptions"
              :key="index"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber" placeholder="请输入工号" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-popover
            v-model="selectDeptVisible"
            placement="bottom"
            width="345"
            trigger="manual"
          >
            <el-tree :data="deptList" :props="{label: 'name'}" default-expand-all @node-click="handleNodeClick"></el-tree>
            <el-input slot="reference" v-model="form.departmentName" placeholder="请输入部门" clearable :style="{width: '100%'}" @click.stop.native @focus="selectDeptVisible = true">
            </el-input>
          </el-popover>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{width: '100%'}"
            placeholder="请选择转正时间"
            clearable
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <el-row slot="footer" justify="center" type="flex">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import employeeEnum from '@/api/constant/employees.js'
import { getDepartments } from '@/api/departments'
import { translateListToTree } from '@/utils'
import { addEmployee } from '@/api/employees'
export default {
  components: {},
  inheritAttrs: false,
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        username: undefined,
        mobile: undefined,
        timeOfEntry: null,
        formOfEmployment: undefined,
        workNumber: undefined,
        departmentName: undefined,
        correctionTime: null
      },
      rules: {
        username: [],
        mobile: [],
        timeOfEntry: [],
        formOfEmployment: [],
        workNumber: [],
        departmentName: [],
        correctionTime: []
      },
      formOfEmploymentOptions: employeeEnum.hireType,
      deptList: [],
      selectDeptVisible: false
    }
  },
  computed: {},
  watch: {},
  async created() {
    /*
    const depts = (await getDepartments()).depts
    this.deptList = translateListToTree(depts, '')
    */
    this.deptList = translateListToTree((await getDepartments()).depts, '')
  },
  mounted() {
    console.log('$options', this.$options)
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
      this.form.departmentName = data.name
      this.selectDeptVisible = false
    },
    onOpen() {},
    close() {
      // 手动复制代码重置表单数据
      /*
      this.form = {
        username: undefined,
        mobile: undefined,
        timeOfEntry: null,
        formOfEmployment: undefined,
        workNumber: undefined,
        departmentName: undefined,
        correctionTime: null
      }
      */
      // 通过$options获取原始配置
      this.form = this.$options.data().form
      this.$refs['formRef'].resetFields()
      this.$emit('update:dialogVisible', false)
    },
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        // 请求接口
        await addEmployee(this.form)
        // 提示成功
        this.$message.success('操作成功')
        // 刷新列表
        this.$emit('success')
        this.close()
      })
    }
  }
}

</script>
<style lang="scss" scoped>
::v-deep .el-tree-node__expand-icon,
::v-deep .el-tree-node__expand-icon.expanded{
  display: none !important;
}
</style>
